import { queryAllSystemData, queryAllGuestSystemData } from '@/api/portal/manager/analysis'
import { getToken } from '@/utils/auth'
import { queryAllServiceNoAuthByOrganization, queryAllServiceAuthByOrganization } from '@/api/portal/manager/setting/index'
import { requestGet } from '@/api/system/common'
const pickerOptions = {
  shortcuts: [
    {
      text: '最近一周',
      onClick(picker) {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
        picker.$emit('pick', [start, end])
      }
    },
    {
      text: '最近一个月',
      onClick(picker) {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
        picker.$emit('pick', [start, end])
      }
    },
    {
      text: '最近三个月',
      onClick(picker) {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
        picker.$emit('pick', [start, end])
      }
    }
  ]
}
// 信息列表模版解析
export const template = {
  props: {
    temp: {
      type: Object,
      default() {
        return {}
      }
    },
    showHeader: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      setting: null,
      baseURL: process.env.VUE_APP_BASE_API.substring(
        0,
        process.env.VUE_APP_BASE_API.length - 1
      ),
      column: [],
      item: [],
      dataList: [], // 解析出的数据
      timer: null, // 定时任务
      cid: null, // 数据项ID
      isRefresh: false, // 是否刷新
      refreshTime: 30, // 默认刷新时间
      isOpenEntity: false, // 是否打开详情页
      entityTemplateId: null, // 详情模版编号
      entityOpenType: null, // 详情页打开方式
      entityParams: [], // 查询详情参数
      entityUrl: '', // 详情地址
      pagerSize: 0,
      contentStyle: {}, // 模块样式
      borderColumn: {}, // 内容区域的样式
      // borderStyle: {}, // 栏目模块的边框
      pickerOptions: pickerOptions,
      form: {
        page: 1,
        size: 10,
        cid: null,
        blurry: ''
      }
    }
  },

  watch: {
    temp: {
      handler(newData, oldData) {
        if (this.showHeader === true) {
          const pzx = JSON.parse(newData.data.pzx)
          this.setting = pzx
          this.column = pzx.data
          this.item = pzx.item
          this.cid = newData.data.id
          if (pzx.isRefresh !== undefined && pzx.isRefresh === true) {
            this.isRefresh = pzx.isRefresh
          }
          if (pzx.refreshTime !== undefined && pzx.refreshTime !== null) {
            this.refreshTime = pzx.refreshTime
          }
        } else {
          const pzx = JSON.parse(newData.pzx)
          this.setting = pzx
          if (pzx.isRefresh !== undefined && pzx.isRefresh === true) {
            this.isRefresh = pzx.isRefresh
          }
          if (pzx.refreshTime !== undefined && pzx.refreshTime !== null) {
            this.refreshTime = pzx.refreshTime
          }
          this.column = pzx.data
          this.item = pzx.item
          this.cid = newData.id
        }
        if (this.column === undefined) {
          this.column = []
        }
        this.initData()

        if (newData.showHeader === true) {
          this.contentStyle.padding = '0'
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  computed: {
    borderStyle() {
      if (this.temp.sfxsbk === true) {
        return {
          border: '1px solid  var(--edu-border-100)',
          backgroundColor: 'var(--edu-bk-100)',
          color: 'var(--edu-text-100)',
          borderRadius: this.layoutTheme.borderRadius
        }
      }
      if (this.temp.bkyy === true) {
        return {
          boxShadow: '0 2px 4px rgba(0, 0, 0, .12)',
          backgroundColor: 'var(--edu-bk-100)',
          color: 'var(--edu-text-100)',
          borderRadius: this.layoutTheme.borderRadius
        }
      }
      return {
        backgroundColor: 'var(--edu-bk-100)',
        color: 'var(--edu-text-100)',
        borderRadius: this.layoutTheme.borderRadius
      }
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  methods: {
    initData() {
      if (getToken()) {
        queryAllSystemData({ cid: this.cid })
          .then((res) => {
            const { data } = res
            if (undefined !== data.dataList && data.dataList !== null) {
              this.dataList = data.dataList
              if (this.isRefresh === true) {
                this.timer = setTimeout(this.initData, this.refreshTime * 1000)
              }
            } else if (undefined !== data.data && data.data !== null) {
              this.dataList = data.data
              if (this.isRefresh === true) {
                this.timer = setTimeout(this.initData, this.refreshTime * 1000)
              }
            }
          })
          .catch(() => {
            this.dataList = []
          })
      } else {
        queryAllGuestSystemData({ cid: this.cid })
          .then(res => {
            const { data } = res
            if (undefined !== data.dataList && data.dataList !== null) {
              this.dataList = data.dataList
              if (this.isRefresh === true) {
                this.timer = setTimeout(this.initData, this.refreshTime * 1000)
              }
            } else if (undefined !== data.data && data.data !== null) {
              this.dataList = data.data
              if (this.isRefresh === true) {
                this.timer = setTimeout(this.initData, this.refreshTime * 1000)
              }
            }
          })
          .catch(() => {
            this.dataList = []
          })
      }
    },
    callBack() {
      if (this.temp.dkfs === 2) {
        // 自定义打开方式
        if (undefined !== this.temp.path && this.temp.path !== null) {
          window.open(this.temp.path, '_blank')
        }
      } else if (this.temp.dkfs === 1) {
        this.$emit('fetch-data', this.temp)
      } else {
        console.log('')
      }
    },
    queryEntity() {
      this.form.page = 1
      this.initData()
    },
    handleCurrentChange(obj) {
      this.form.page = obj
      this.initData()
    },
    handleSizeChange(obj) {
      this.form.size = obj
      this.form.page = 1
      this.initData()
    },
    closeAlert(index) {
      this.active = null
    },
    openAlert(index) {
      this.active = index
    },
    openMore() {
      if (this.temp.dkfs === 2) {
        // 自定义打开方式
        if (undefined !== this.temp.path && this.temp.path !== null) {
          window.open(this.temp.path, '_blank')
        }
      } else if (this.temp.dkfs === 1) {
        this.$store
          .dispatch('GetThemes')
          .then(res => {
            return true
          })
          .catch(error => {
            console.log(error)
          })
        this.$emit('fetch-data', this.temp)
      } else {
        console.log('')
      }
    },
    // 点击详情页
    openEntity(data) {
      let title = ''
      if (this.setting.isFixTitle) {
        title = this.setting.entityTitle
      } else {
        title = data[this.setting.entityTitleCode]
      }
      const entity = {
        isOpenEntity: this.setting.isOpenEntity, // 是否打开详情页
        entityParams: this.setting.entityParams, // 详情页查询参数
        params: this.setting.params, // 详情页打开方式为地址参数时，参数设置
        openType: this.setting.entityOpenType, // 详情页打开方式
        url: this.setting.entityUrl, // 详情地址
        temp: this.setting.entityTemp, // 详情模版
        entity: data, // 详情数据
        title: title // 标题
      }
      this.$bus.emit('openEntity', { data: entity })
    },
    // 点击统计项，打开类似更多模版
    openVisitingCard(data) {
      if (data.isOpenMore) {
        const entity = {
          id: data.temp,
          name: this.temp.lmmc, // 栏目名称
          dkfs: data.openType, // 打开方式
          url: data.openUrl, // 打开地址
          parentId: this.temp.column ? this.temp.column.menu.id : this.temp.columnId, // 父节点编号
          parentName: this.temp.column ? this.temp.column.menu.name : '', // 父节点名称
          isLiveBos: this.temp.isLiveBos // 是否是livebos
        }
        this.$bus.emit('openMore', entity)
      }
    }
  }
}

/**
 * 更多页面
 */
export const more = {
  props: {
    temp: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      pagerSize: 0, // 一共多少条数据
      setting: null, // 配置项
      query: [], // 查询项
      column: [], // 标题项
      dataList: [], // 数据项
      dataListOne: [], // 分项数据
      dataListTow: [], // 分项数据
      borderStyle: {}, // 栏目模块的边框
      isShow: true, // 查询选项
      isBack: true, // 返回
      baseURL: process.env.VUE_APP_BASE_API.substring(
        0,
        process.env.VUE_APP_BASE_API.length - 1
      ),
      form: {
        page: 1,
        size: 10, // 控制更多页面显示数量2
        cid: null,
        title: ''
      }
    }
  },
  watch: {
    temp: {
      handler(newName, oldName) {
        // 值发生变化时
        if (undefined !== newName && newName !== null) {
          console.log(newName.isBack)
          if (newName.isBack !== undefined) {
            this.isBack = newName.isBack
          }
          const setting = JSON.parse(newName.pzx)
          this.setting = setting
          if (setting.data !== undefined && setting.data !== null) {
            this.column = setting.data
          }
          if (setting.query !== undefined && setting.query !== null) {
            this.query = setting.query
          }
          this.form.cid = newName.id
          this.initData()
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    themes: {
      handler(val, oldVal) {
        if (val) {
          this.borderStyle.borderRadius = val.bkdx
          if (this.temp.sfxsbk === true) {
            this.borderStyle.border = '1px solid ' + val.bkys
          }
          if (this.temp.bkyy === true) {
            this.borderStyle.boxShadow = '0 2px 4px rgba(0, 0, 0, .12)'
          }
          this.borderStyle.background = val.columnGroundColor
          this.borderStyle.color = val.columnTitleFontColor
          this.borderStyle = Object.assign({}, this.borderStyle)
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  computed: {
    themes() {
      return this.$store.getters.theme
    }
  },
  methods: {
    // 第几页
    handleCurrentChange(obj) {
      this.form.page = obj
      this.initData()
    },
    // 每页显示多少条数据
    handleSizeChange(obj) {
      this.form.size = obj
      this.form.page = 1
      this.initData()
    },
    // 点击查询按钮
    queryEntity() {
      this.form.page = 1
      this.initData()
    },
    // 初始化数据
    initData() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(255, 255, 255, 0.7)'
      })
      let url = 'rest/api/guest/temp/v2/column/analysis/sys'
      if (getToken()) {
        url = 'rest/api/temp/v2/column/analysis/sys'
      }
      requestGet(url, this.form)
        .then(res => {
          const { data } = res
          if (undefined !== data.pager && data.pager !== null) {
            this.pagerSize = data.pager.totalElements
          }
          if (undefined !== data.data && data.data !== null) {
            this.dataList = data.data
          } else if (undefined !== data.dataList && data.dataList !== null) {
            this.dataList = data.dataList
          }
          if (this.dataList.length > 0) {
            const dataObject = JSON.stringify(this.dataList)
            this.$nextTick(() => {
              const newData = JSON.parse(dataObject)
              const middleIndex = Math.ceil(newData.length / 2)
              this.dataListOne = newData.splice(0, middleIndex)
              this.dataListTow = newData.splice(-middleIndex)
            })
          }
        })
        .catch(() => {
          this.dataList = []
        }).finally(() => {
          this.$bus.emit('scrollHeight')
          loading.close()
        })
    },
    // 打开详情页
    openEntity(data) {
      let title = ''
      if (this.setting.isFixTitle) {
        title = this.setting.entityTitle
      } else {
        title = data[this.setting.entityTitleCode]
      }
      const entity = {
        isOpenEntity: this.setting.isOpenEntity, // 是否打开详情页
        entityParams: this.setting.entityParams, // 详情页查询参数
        params: this.setting.params, // 详情页打开方式为地址参数时，参数设置
        openType: this.setting.entityOpenType, // 详情页打开方式
        url: this.setting.entityUrl, // 详情地址
        temp: this.setting.entityTemp, // 详情模版
        entity: data, // 详情数据
        title: title // 标题
      }
      this.$bus.emit('openEntity', { data: entity })
    },
    // 查询返回数据
    queryBack(data) {
      if (undefined !== data) {
        const obj = this.query.filter(item => {
          if (data.id === item.sjx) {
            return item
          }
        })
        this.form[obj[0].column] = data.value
        this.initData()
      }
    },
    back() {
      this.$router.go(-1)
    }
  }
}

/**
 * 办事大厅模版,mixins 混入内容
 * @type {{data(): {baseURL: string, dataList: [], query: {blurry: string}, column: [], queryList: [], setting: []}, computed: {borderStyle(): ({border: string, backgroundColor: service.computed.layoutTheme.menuBackground, color: service.computed.layoutTheme.menuColor})}, watch: {temp: {handler(*, *): void, deep: boolean, immediate: boolean}}, methods: {initData(): void, queryObject(): void, queryClick(*): void}, props: {temp: {default(): {}, type: ObjectConstructor}, showHeader: {default: boolean, type: BooleanConstructor}}}}
 */
export const service = {
  props: {
    temp: {
      type: Object,
      default() {
        return {}
      }
    },
    showHeader: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      dataList: [], // 服务数据
      column: [], // 配置信息
      queryList: [], // 查询条件列表
      baseURL: process.env.VUE_APP_BASE_API.substring(
        0,
        process.env.VUE_APP_BASE_API.length - 1
      ),
      query: {
        blurry: ''
      },
      setting: []
    }
  },

  computed: {
    borderStyle() {
      if (this.temp.sfxsbk === true) {
        return {
          border: '1px solid var(--edu-border-100)',
          borderRadius: this.layoutTheme.borderRadius
        }
      }
      if (this.temp.bkyy === true) {
        return {
          boxShadow: '0 2px 4px rgba(0, 0, 0, .12)',
          borderRadius: this.layoutTheme.borderRadius
        }
      }
      return {
        borderRadius: this.layoutTheme.borderRadius
      }
    }
  },
  watch: {
    temp: {
      handler(newData, oldData) {
        this.query = {
          blurry: ''
        }
        if (this.showHeader) {
          const pzx = newData.data.pzx !== '' ? JSON.parse(newData.data.pzx) : []
          this.column = pzx
          this.setting = pzx
        } else {
          const pzx = JSON.parse(newData.pzx)
          this.column = pzx
          this.setting = pzx
        }
        if (undefined !== this.column.query && this.column.query !== null) {
          this.queryList = this.column.query
        } else {
          this.queryList = []
        }
        this.initData()
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    queryObject() {
      this.initData()
    },
    initData() {
      if (getToken()) {
        queryAllServiceAuthByOrganization(this.query)
          .then(res => {
            const { data } = res
            this.dataList = data.data
          })
          .catch(() => {
            this.dataList = []
          })
      } else {
        queryAllServiceNoAuthByOrganization(this.query)
          .then(res => {
            const { data } = res
            this.dataList = data.data
          })
          .catch(() => {
            this.dataList = []
          })
      }
    },
    queryClick(obj) {
      this.query[obj.code] = obj.value
      this.initData()
    }
  }
}
export const openEntity = {
  methods: {
    openEntity(data) {
      // 打开详情页
      let title = ''
      if (this.setting.isFixTitle) {
        title = this.setting.entityTitle
      } else {
        title = data[this.setting.entityTitleCode]
      }
      const entity = {
        isOpenEntity: this.setting.isOpenEntity, // 是否打开详情页
        entityParams: this.setting.entityParams, // 详情页查询参数
        params: this.setting.params, // 详情页打开方式为地址参数时，参数设置
        openType: this.setting.entityOpenType, // 详情页打开方式
        url: this.setting.entityUrl, // 详情地址
        temp: this.setting.entityTemp, // 详情模版
        entity: data, // 详情数据
        title: title // 标题
      }
      this.$bus.emit('openEntity', { data: entity })
    }
  }
}

export const statisticsTemp = {
  data() {
    return {
      column: [],
      dataList: [],
      baseURL: process.env.VUE_APP_BASE_API.substring(
        0,
        process.env.VUE_APP_BASE_API.length - 1
      ) // api 的 base_url
    }
  },
  computed: {
    themes() {
      return this.$store.getters.theme
    },
    borderStyle() {
      if (!this.theme) {
        if (this.temp.sfxsbk === true) {
          return {
            border: '1px solid #eeeeee',
            backgroundColor: this.layoutTheme.menuBackground,
            color: this.layoutTheme.menuColor,
            borderRadius: this.layoutTheme.borderRadius
          }
        }
        if (this.temp.bkyy === true) {
          return {
            boxShadow: '0 2px 4px rgba(0, 0, 0, .12)',
            backgroundColor: this.layoutTheme.menuBackground,
            color: this.layoutTheme.menuColor,
            borderRadius: this.layoutTheme.borderRadius
          }
        }
        return {
          backgroundColor: this.layoutTheme.menuBackground,
          color: this.layoutTheme.menuColor,
          borderRadius: this.layoutTheme.borderRadius
        }
      } else {
        if (this.temp.sfxsbk === true) {
          return {
            border: this.theme.bkys,
            backgroundColor: this.theme.columnGroundColor,
            color: this.theme.columnTitleFontColor,
            borderRadius: this.theme.bkdx
          }
        }
        if (this.temp.bkyy === true) {
          return {
            boxShadow: '0 2px 4px rgba(0, 0, 0, .12)',
            backgroundColor: this.theme.columnGroundColor,
            color: this.theme.columnTitleFontColor,
            borderRadius: this.theme.bkdx
          }
        }
        return {
          backgroundColor: this.theme.columnGroundColor,
          color: this.theme.columnTitleFontColor,
          borderRadius: this.theme.bkdx
        }
      }
    },
    liStyle() {
      const width = (100 / this.column.size).toFixed(2)
      const height = (100 / this.column.rows).toFixed(2)
      return { width: width + '%', height: height + '%' }
    }
  },
  watch: {
    temp: {
      handler(newData, oldData) {
        if (this.showHeader) {
          const pzx = JSON.parse(newData.data.pzx)
          this.column = pzx
          this.dataList = pzx.data
        } else {
          const pzx = JSON.parse(newData.pzx)
          this.column = pzx
          this.dataList = pzx.dataList
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    themes: {
      handler(val, oldVal) {
        if (val) {
          this.borderStyle.borderRadius = val.bkdx
          if (this.temp.sfxsbk === true) {
            this.borderStyle.border = '1px solid ' + val.bkys
          }
          if (this.temp.bkyy === true) {
            this.borderStyle.boxShadow = '0 2px 4px rgba(0, 0, 0, .12)'
          }
          this.borderStyle.background = val.columnGroundColor
          this.borderStyle.color = val.columnTitleFontColor
          // this.borderStyle = Object.assign({}, this.borderStyle);
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    callBack(data) {
      if (data.isOpenMore) {
        const entity = {
          id: data.temp,
          name: this.temp.lmmc, // 栏目名称
          dkfs: data.openType, // 打开方式
          url: data.openUrl, // 打开地址
          parentId: this.temp.column.menu.id, // 父节点编号
          parentName: this.temp.column.menu.name // 父节点名称
        }
        this.$bus.emit('openMore', entity)
      }
    },
    openMore() {
      if (this.temp.dkfs === 2) {
        // 自定义打开方式
        if (undefined !== this.temp.path && this.temp.path !== null) {
          window.open(this.temp.path, '_blank')
        }
      } else if (this.temp.dkfs === 1) {
        this.$store
          .dispatch('GetThemes')
          .then(res => {
            return true
          })
          .catch(error => {
            console.log(error)
          })
        this.$emit('fetch-data', this.temp)
      } else {
        console.log('')
      }
    }
  }
}
